package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class GoListener extends Utils implements MouseListener, ActionListener, GameStateObserver {
    Graphics g;
    private WinStrategy winStrategy = new DefaultWinStrategy();

    @Override
    public void actionPerformed(ActionEvent e) {
        String ac = e.getActionCommand();
        acType = ac;
        Command command = CommandFactory.createCommand(ac);
        if (command != null) {
            command.execute(g);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (acType.equals("复盘")) {
            if (replayIndex != chessSize) {
                replay(g, replayIndex);
                replayIndex++;
            } else {
                if (checkWin()) {
                    JOptionPane.showConfirmDialog(null, (chessType == 1 ? "黑方" : "白方") + "胜利,复盘完毕");
                } else {
                    JOptionPane.showConfirmDialog(null, "复盘已完毕，未分出胜负");
                }
            }
        } else {
            if (chessType == 0) {
                if (chessSize != 0) {
                    JOptionPane.showConfirmDialog(null, chessSize % 2 == 0 ? "白方已胜利" : "黑方已胜利");
                }
                return;
            }
            int x = e.getX();
            int y = e.getY();
            modifyData(x, y);
            drawChess(g, chessSize);
            if (checkWin()) {
                JOptionPane.showConfirmDialog(null, (chessType == 1 ? "黑方" : "白方") + "胜利");
                chessType = 0;
                notifyObservers();
            }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void update() {
        // 处理游戏状态更新的逻辑
    }
    @Override
    public boolean checkWin() {
        return winStrategy.checkWin(chessArray, row, col, chessType);
    }
}